<template>
  <div
    class="home"
    v-loading="loading"
    element-loading-text="转换中..."
    :element-loading-spinner="svg"
    element-loading-svg-view-box="-10, -10, 50, 50"
    element-loading-background="rgba(255, 255, 255, 0.6)"
  >
    <div class="header">
      <el-button type="success" size="medium" @click="deal">转换</el-button>
      <el-button type="primary" size="medium" @click="copy">复制</el-button>
    </div>
    <div class="left">
      <el-input v-model="text" placeholder="请输入vue代码" type="textarea" autofocus />
    </div>
    <div class="right">
      <!-- <el-input v-model="output" type="textarea" /> -->
      <Prism language="html" v-if="output">
        {{ output }}
      </Prism>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from "vue"
import axios from "axios"
import Prism from "vue-prism-component"
import { ElMessage } from "element-plus"
export default defineComponent({
  components: {
    Prism
  },
  setup() {
    const state = reactive({
      isCompositionHome: false
    })
    const text = ref("")
    const output = ref("")
    const loading = ref(false)
    let value = ""
    const deal = () => {
      if (!text.value) {
        ElMessage({
          message: "请输入vue代码",
          grouping: true,
          type: "error"
        })
        return
      }
      output.value = ""
      loading.value = true
      axios
        .post("/api/", { a: text.value })
        .then(function(response) {
          loading.value = false
          if (response.data.error) {
            ElMessage({
              message: response.data.error,
              grouping: true,
              type: "error"
            })
            return
          }
          output.value = response.data.title
          value = response.data.title
        })
        .catch(() => {
          loading.value = false
        })
    }
    // 复制功能1
    const copy = () => {
      if (!value) {
        return
      }
      let isSuccess = false
      let target: any = document.createElement("div")
      target.id = "tempTarget"
      target.style.opacity = "0"
      target.innerText = value
      document.body.appendChild(target)
      try {
        let range = document.createRange()
        range.selectNode(target)
        window?.getSelection()?.removeAllRanges?.()
        window?.getSelection()?.addRange?.(range)
        document.execCommand("copy")
        window?.getSelection()?.removeAllRanges()
        isSuccess = true
      } catch (e) {
        console.error("复制失败")
        ElMessage({
          message: "复制失败.",
          grouping: true,
          type: "success"
        })
        return
      }

      if (value) {
        // remove temp target
        target.parentElement.removeChild(target)
      }
      ElMessage({
        message: "复制成功.",
        grouping: true,
        type: "success"
      })
      return isSuccess
    }

    return { ...state, text, output, loading, deal, copy }
  }
})
</script>
<style lang="scss" scoped>
$border-color: #bbbbbb;
.home {
  display: grid;
  grid-template-columns: 40%;
  grid-template-rows: 44px 1fr;
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  height: 100%;
  border: 1px solid $border-color;
  box-sizing: border-box;
  ::v-deep(.el-textarea__inner) {
    box-shadow: none;
    border-radius: 0;
    height: 100%;
  }
  ::v-deep(.el-textarea) {
    --el-input-height: 100%;
    height: 100%;
  }
}
.header {
  grid-column: 1 / span 2;
  border-bottom: 1px solid $border-color;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}
.left {
  border-right: 1px solid $border-color;
}
.right {
  overflow: auto;
}
</style>
